<?php

namespace app\api\controller;

use app\api\model\User;
use think\Controller;
use think\Debug;
use think\Request;

class ActivateMember extends Base
{
    public function index_redirect()
    {
//        echo "helloWord";
//        $params = input("param.");
//        var_dump($params);
//        $cardId = input('card_id');
//        $encryptCode = input('encrypt_code');
//        $openId = input('openid');
        $activateTicket = input('activate_ticket');
        $wechatCard = new \app\mpbase\library\WechatCard();
        $res = $wechatCard->getActivateTempInfo($activateTicket);
        if($res['errcode'] == 0 && $res['errmsg'] == 'ok'){
            $userInfo = [];
            $commonFieldList = $res['info']['common_field_list'];
            foreach ($commonFieldList as $cfield) {
                if($cfield['name'] != 'USER_FORM_INFO_FLAG_POST_CODE'){
                    $fieldName =  strtolower(substr($cfield['name'],20));
                    $userInfo[$fieldName]= $cfield['value'];
                }
            }
            $user = model('user');
            $r = $user->allowField(true)->save($userInfo);
            if($r>0)
                return self::apiResp(['msg'=>'添加成功']);
        }
        return json($res);
    }

    //Xdebug断点调试 拼接ideKey
    public function index()
    {
        $url = str_replace('index','index_redirect',request()->url(true))."&XDEBUG_SESSION_START=PHPSTORM";
        $xmlData = file_get_contents('php://input');
        $result = curl_post_raw($url,$xmlData);
        return $result;
    }
}

function curl_post_raw($url, $rawData)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}